﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using volva.Services;
using volva.Models;

namespace volva.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Forsíða";
            ViewData["MostPopularStations"] = StationService.FindMostPopularStations();
            ViewData["MostPopularVideos"] = StationService.FindMostPopularVideos(4);
            ViewData["ShowcaseVideos"] = StationService.FindMostPopularVideos(6);
            ViewData["MostRecentStations"] = StationService.FindMostRecentStations();

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
        
        public ActionResult Watch(int id)
        {
            ViewData["MostPopularVideos"] = StationService.FindMostPopularVideos(4);

            
            Video model = StationService.GetVideoByID(id);
            StationService.AddVideoView(id);
            StationService.AddStationView(model.Station_id);

            ViewData["comment"] = StationService.GetVideoComment(id);

            return View(model);
        }
        /// <summary>
        /// A Method that searches for videos
        /// </summary>
        /// <returns>search results</returns>
        public ActionResult Search()
        {
            if (!String.IsNullOrEmpty(Request.Form["query"]))
            {
                string query = Request.Form["query"];
                bool NSFW = false;
                ViewData["VideoResults"] = SearchService.SearchVideo(query, NSFW);
                ViewData["StationResults"] = SearchService.SearchStation(query);
                ViewData["SeriesResults"]= SearchService.SearchSeries(query);
            }

            return View();
        }
        /// <summary>
        /// A Method that lists series
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowSeriesList()
        {
            ViewData["SeriesList"] = SeriesService.GetList();
            return View();
        }
        public ActionResult Series(int id)
        {
            Series model = SeriesService.getSerieById(id);
            ViewData["videosInSeries"] = SeriesService.videosInSerie(id);
            return View(model);
        }
        public ActionResult MakeComment(int id)
        {
            string comment = Request.Form["comment"];
            int userId = AccountService.GetUserIDByName(User.Identity.Name);

            StationService.AddVideoComment(comment, userId, id);

            return View();
        }

    }
}
